/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-09-08
 * Time: 19:19
 */
public class Test { public ListNode removeZeroSumSublists(ListNode head) {
    return eraseNode(head);
}

    public ListNode eraseNode(ListNode node) {
        if (node == null) {
            return null;
        }


        int sum = 0;
        ListNode cur = null;
        for (cur = node; cur != null; cur = cur.next) {
            sum += cur.val;
            if (sum == 0) {
                return eraseNode(cur.next);
            }
        }

        node.next = eraseNode(node.next);
        return node;
    }

    public ListNode removeZeroSumSublists(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        Map<Integer, ListNode> map = new HashMap<>();
        int sum = 0;
        for (ListNode cur = dummy; cur != null; cur = cur.next) {
            sum += cur.val;
            map.put(sum, cur);
        }
        sum = 0;
        for (ListNode cur = dummy; cur != null; cur = cur.next) {
            sum += cur.val;
            cur.next = map.get(sum).next;
        }
        return dummy.next;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int sum = carry;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            current.next = new ListNode(sum % 10);
            current = current.next;
            carry = sum / 10;
        }
        return dummy.next;
    }

    public static void main(String[] args) {

    }
}
